package com.zg.autoform.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoform.web.req.ListImportRecordReq;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoform.DiyImportRecord;
import com.zg.common.core.dao.autoform.DiyImportRecordDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 导入记录查询控制器
 * @author zg
 */
@RestController
@RequestMapping("/import/record")
@RequiredArgsConstructor
@Api(tags = {"导入记录管理"})
public class ImportRecordController {

    private final DiyImportRecordDao recordDao;

    @ApiOperation("查询导入记录列表")
    @PostMapping("/list")
    public R<IPage<DiyImportRecord>> listImportRecords(@RequestBody ListImportRecordReq req) {
        Page<DiyImportRecord> page = new Page<>(req.getPage(), req.getPageSize());

        LambdaQueryWrapper<DiyImportRecord> wrapper = new LambdaQueryWrapper<DiyImportRecord>()
                .eq(req.getTemplateId() != null, DiyImportRecord::getTemplateId, req.getTemplateId())
                .eq(req.getStatus() != null, DiyImportRecord::getStatus, req.getStatus())
                .orderByDesc(DiyImportRecord::getCreateTime);

        IPage<DiyImportRecord> result = recordDao.page(page, wrapper);
        return R.success(result);
    }

    @ApiOperation("查询导入记录详情")
    @PostMapping("/detail")
    public R<DiyImportRecord> getImportRecordDetail(@RequestParam String batchNo) {
        DiyImportRecord record = recordDao.getOne(
            new LambdaQueryWrapper<DiyImportRecord>()
                .eq(DiyImportRecord::getBatchNo, batchNo)
        );

        if (record == null) {
            return R.error("导入记录不存在");
        }

        return R.success(record);
    }
}
